import {reqRegister} from "../../request/api"
// 初始值
const initState = {
    // 初始化user
    user: {
        phone: "",
        nickname: "",
        password: ""
    }
}

// actionsTypes
const types = {
    // 修改user type
    CHANGE_USER: "REGISTER_CHANGE_USER",
}

// reducer 修改数据
const reducer = (state = initState, action) => {
    switch (action.type) {
        case types.CHANGE_USER:
            return {
                ...state,
                user: {
                    ...state.user,
                    [action.key]: action.value
                }
            }
        default:
            return state;
    }

}

// actionCreators组件派发的动作
export const actions={
    // 修改user的action
    changeUser:(key,value)=>({type:types.CHANGE_USER,key,value}),
    // 注册请求
    reqRegister:(history)=>(dispatch,getState)=>{
        let { user } = getState().register;
        reqRegister(user).then(res => {
            if (res.data.code === 200) {
                //跳转页面
                history.push("/login")
            }
        })
    }
}

//selectors导出数据
export const getUser=state=>state.register.user;

export default reducer